package com.mew.lobby.handler.login;

import com.mew.common.dc.db.UserService;
import com.mew.common.dc.redis.AucRedisService;
import com.mew.common.entity.user.User;
import com.mew.common.exception.ResponseCode;
import com.mew.common.rest.bo.MewSession;
import com.mew.common.util.StringUtils;
import com.mew.lobby.config.LobbySession;
import com.mew.lobby.config.LobbySessionManager;
import com.mew.lobby.config.MewGameConfig;
import com.mew.lobby.message.request.EnterLobbyRequestMessage;
import com.mew.lobby.provide.LoginExitFeignService;
import com.zebra.remoting.connection.Connection;
import com.zebra.remoting.tms.TmsConfigInfoManager;
import com.zebra.remoting.protocol.ZebraCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class EnterLobbyService {

    @Autowired
    private AucRedisService aucRedisService;
    @Autowired
    private MewGameConfig mewGameConfig;
    @Autowired
    private UserService userService;
    @Autowired
    private LoginExitFeignService loginExitFeignService;


    public void enterLobby(ZebraCommand<EnterLobbyRequestMessage> command, MewSession session){
        EnterLobbyRequestMessage requestMessage = command.getRequest();
        boolean check = StringUtils.checkMewStr(requestMessage.getEncryptionStr(), mewGameConfig.getLobbyKey(), mewGameConfig.getLobbyCode(), command.getToken(), requestMessage.getStartTime());
        if (!check){
            command.sendFailureCode(ResponseCode.ParamInvalid.code());
            return;
        }
        doEnter(command);

    }

    public void reconnectionLobby(ZebraCommand<EnterLobbyRequestMessage> command, MewSession session){

        EnterLobbyRequestMessage requestMessage = command.getRequest();
        boolean check = StringUtils.checkMewStr(requestMessage.getEncryptionStr(), mewGameConfig.getLobbyKey(), mewGameConfig.getLobbyCode(), command.getToken(), requestMessage.getStartTime());
        if (!check){
            command.sendFailureCode(ResponseCode.ParamInvalid.code());
            return;
        }

        if (!session.checkChessPlaying() && session.rioid != -9){
            loginExitFeignService.exitGame(command.getUserId());
        }

        doEnter(command);
    }

    private void doEnter(ZebraCommand<EnterLobbyRequestMessage> command){
        User user = userService.getRedisUserById(command.getUserId());
        if (null == user){
            command.sendFailureCode(ResponseCode.LoginUn.code());
            return;
        }

        Connection connection = Connection.getByChannel(command.getChannel());
        int gwId = null == connection ? 0 : connection.getServerId();
        int serverId = TmsConfigInfoManager.getInstance().getServerInfo().getServerId();
        long token = aucRedisService.getToken();

        MewSession other = new MewSession();
        other.tk = token;
        other.gwsid = gwId;
        other.lsid = serverId;
        other.loc = 0;
        aucRedisService.onlineSession(command.getUserId(), other);
        command.setToken(token);
        command.sendSuccessMsg();


        LobbySession lobbySession = LobbySessionManager.getInstance().getSession(command.getUserId());
        lobbySession = null == lobbySession ? new LobbySession() : lobbySession;
        lobbySession.setChannel(command.getChannel());
        lobbySession.setTk(token);
        lobbySession.setUserId(command.getUserId());
        lobbySession.setWhiteList(user.getWhiteList());
        LobbySessionManager.getInstance().putSession(lobbySession);

    }

    public void exit(long userId, int exitType){
        loginExitFeignService.exitLobby(userId, exitType);
    }
}
